<?php
namespace app\common\server;
use x\Xbase;
class Usercountday extends Xbase{

    //统计用户今天之前未统计的收益
    public function doit($user){
        $this->db->begin();
        $mysql = [];
        $total = 0;
        $t = strtotime(date("Y-m-d",time())." 0:0:0");

#=============================================统计收益=============================================
        #代理
        $daili = $this->db->query("select sum(money) as total from award_daili where user_id=? and is_count=0 and create_time<?", [$user['id'],$t],1);
        if($daili['total']){
          $total = bcadd($total, $daili['total'],2);
          $mysql[] = $this->db->query("update award_daili set is_count=1 where user_id=? and is_count=0 and create_time<?", [$user['id'],$t]);
        }
        #推广
        $tuiguang = $this->db->query("select sum(money) as total from award_tuiguang where user_id=? and is_count=0 and create_time<?", [$user['id'],$t],1);
        if($tuiguang['total']){
          $total = bcadd($total, $tuiguang['total'],2);
          $mysql[] = $this->db->query("update award_tuiguang set is_count=1 where user_id=? and is_count=0 and create_time<?", [$user['id'],$t]);
        }
        #分红
        $fenhong = $this->db->query("select sum(money) as total from award_fenhong where user_id=? and is_count=0 and create_time<?", [$user['id'],$t],1);
        if($fenhong['total']){
          $total = bcadd($total, $fenhong['total'],2);
          $mysql[] = $this->db->query("update award_fenhong set is_count=1 where user_id=? and is_count=0 and create_time<?", [$user['id'],$t]);
        }
        //插入用户每日收益表
        if($total){
          $d = [
            'user_id' => $user['id'],
            's_id' => $user['s_id'],
            'money' => $total,
            'ymd' => date("Ymd"),
            'create_time' => time(),
          ];
          $mysql[] = $this->db->insert("user_count_day",$d);
        }


#=============================================统计分红=============================================
        $total_fenhong = $this->db->query("select sum(money) as total from user_count_day where is_count=0 and s_id=? and create_time<?", [$user['id'],$t],1);
        if($total_fenhong['total']){
            $money = bcmul($total_fenhong['total'], 0.2,2);
            if( $money>0 ){
                $d = [
                  'user_id' => $user['id'],
                  'money'   => $money,
                  'create_time' => time(),
                ];
                $mysql[] = $id = $this->db->insert("award_fenhong", $d);
                $mysql[] = m('userbill','c')->doit('', $d['user_id'], 102,'award_fenhong', $id,$money,'money','分红收益');
                $mysql[] = $this->db->query("update user_count_day set is_count=1 where is_count=0 and s_id=? and create_time<?", [$user['id'],$t]);
            }
        }

        #更新用户统计时间
        $d = [
          'id' => $user['id'],
          'count_day' => date('Ymd'),
        ];
        $mysql[] = $this->db->update("user", $d);
        $rs = $this->db->end($mysql);
        if (!$rs) {
          return $this->setError('统计失败');
        }
        return true;
    }
}
